<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i>
                    {{pageTitle}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container" :style="{'min-height': containerHeight + 'px'}">
            <div class></div>
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-folder-add" @click="goAdd">新增</el-button>
                <el-button type="warning" icon="el-icon-edit" @click="goEdit">修改</el-button>
                <el-button type="danger" icon="el-icon-delete" @click="delAllSelection">删除</el-button>
            </div>
            <!-- 搜索表单 -->
            <el-form :model="query" ref="searchRuleForm" label-width="100px">
                <el-row>
                    <el-col :sm="20" :md="8">
                        <el-form-item label="试用到期:">
                            <el-date-picker
                                v-model="tryTime"
                                type="daterange"
                                range-separator="至"
                                start-placeholder="开始日期"
                                end-placeholder="结束日期"
                                @change="tryTimeChange"
                            ></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :sm="20" :md="5">
                        <el-form-item label="员工:" prop="employeeName">
                            <div @click="showTreeDialog('员工')">
                                <el-input v-model="query.employeeName" placeholder="请输入" readonly>
                                    <i slot="suffix" class="el-icon-search"></i>
                                </el-input>
                            </div>
                        </el-form-item>
                    </el-col>
                    <el-col :sm="20" :md="5">
                        <el-form-item label="合同状态:" prop="contractStatusId">
                            <el-select
                                v-model="query.contractStatusId"
                                placeholder="请选择"
                                clearable
                                @focus="dictItemForPerfection('合同状态')"
                            >
                                <el-option
                                    v-for="(item,index) in contractStatus"
                                    :key="index"
                                    :label="item.itemName"
                                    :value="item.id"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :sm="20" :md="8">
                        <el-form-item label=" 合同到期:">
                            <el-date-picker
                                v-model="overTime"
                                type="daterange"
                                range-separator="至"
                                start-placeholder="开始日期"
                                end-placeholder="结束日期"
                                @change="overTimeChange"
                            ></el-date-picker>
                        </el-form-item>
                    </el-col>

                    <el-col :sm="20" :md="5">
                        <el-form-item label="合同类型:" prop="contractTypeId">
                            <el-select
                                v-model="query.contractTypeId"
                                placeholder="请选择"
                                @focus="dictItemForPerfection('合同类型')"
                                clearable
                            >
                                <el-option
                                    v-for="(item,index) in contractType"
                                    :key="index"
                                    :label="item.itemName"
                                    :value="item.id"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :sm="20" :md="6" align="center">
                        <el-button
                            type="primary"
                            icon="el-icon-search"
                            @click="submitForm('searchRuleForm')"
                        >搜索</el-button>
                        <el-button type="info" @click="resetForm('searchRuleForm')">重置</el-button>
                    </el-col>
                </el-row>
            </el-form>

            <div class="tree-box">
                <div class="table-box">
                    <table-List
                        ref="tableList"
                        :tableData="tableData"
                        :tableHeader="tableHeader"
                        :loading="loading"
                        :pagination="pagination"
                        @pageChange="pageChange"
                        @selectFun="selectFun"
                        @sortChange="sortChange"
                    ></table-List>
                </div>
            </div>
        </div>
        <!-- 查询 -->
        <tree-search
            :isShow="showSearch"
            :dialogTitle="searchDialogTit"
            @edit="searchBack"
            @close="closeTreeDialog"
        ></tree-search>
    </div>
</template>

<script>
import appMain from '../../../utils/app_main';
export default {
    name: 'basetable',
    data() {
        return {
            pageTitle: '',
            containerHeight: '',
            //试用到期
            tryTime: [],
            //合同到期
            overTime: [],
            //下拉框数据
            contractStatus: [],
            contractType: [],
            query: {
                contractStatusId: '', // 合同状态id
                contractTypeId: '', // 合同类别id
                employeeName: '', // 员工名字
                employeeId: '', // 员工ID
                overEndTime: '', // 合同到期结束查询时间
                overStartTime: '', // 合同到期开始查询时间
                sortColumn: '', // 排序字段 倒序追加 columnName desc
                tryEndTime: '', // 试用期到期结束查询时间
                tryStarTime: '' // 试用期到期开始查询时间
            },
            loading: false,
            // 分页信息
            pagination: {
                pageIndex: 0,
                pageSize: 10,
                pageTotal: 0
            },

            tableData: [],
            tableHeader: [
                // 表头数据
                {
                    prop: 'employeeName',
                    label: '员工',
                    minWidth: '100px',
                    sortable: 'custom'
                },
                {
                    prop: 'contractTypeName',
                    label: '合同类型',
                    sortable: 'custom'
                    // formatData: function(val) {
                    //     return val.substr(2, 1) == '1' ? '失效' : '正常';
                    // }
                },
                {
                    prop: 'signTime',
                    label: '签约时间',
                    minWidth: '100px',
                    formatData: function(val) {
                        let date = new Date(val);
                        return appMain.formatDate(date, 'yyyy-MM-dd');
                    },
                    sortable: 'custom'
                },
                {
                    prop: 'tryEndtime',
                    label: '试用结束时间',
                    formatData: function(val) {
                        let date = new Date(val);
                        return appMain.formatDate(date, 'yyyy-MM-dd');
                    },
                    sortable: 'custom'
                },
                { prop: 'contractStatusName', label: '合同状态', sortable: 'custom' },
                {
                    prop: 'endTime',
                    label: '失效时间',
                    minWidth: '100px',
                    formatData: function(val) {
                        let date = new Date(val);
                        return appMain.formatDate(date, 'yyyy-MM-dd');
                    },
                    sortable: 'custom'
                },
                {
                    prop: 'active',
                    label: '详情',
                    fixed: 'right',
                    active: [
                        { name: '合同详情', router: 'contractDetail', routerId: 'id' } // router要跳转的
                    ]
                }
            ],
            multipleSelection: [],

            editVisible: false,
            searchDialogTit: '',
            showSearch: false
        };
    },
    created() {
        this.getData();
    },
    methods: {
        // 选择姓名
        showTreeDialog(tit) {
            console.log('showTreeDialog');
            this.searchDialogTit = tit;
            this.showSearch = true;
        },
        getData() {
            this.loading = true;
            console.log('getData', this.loading);
            let params = {
                pageNo: this.pagination.pageIndex,
                pageSize: this.pagination.pageSize,
                record: this.query
            };

            appMain
                .contractFindAllPageByDTO(params)
                .then(res => {
                    if (res.data.code === 20000) {
                        //TODO 测试环境使用
                        this.timer = setTimeout(() => {
                            //设置延迟执行
                            this.loading = false;
                        }, 1000);

                        this.tableData = res.data.data.list;
                        this.pagination.pageSize = res.data.data.pageSize;
                        this.pagination.pageIndex = res.data.data.pageNum;
                        this.pagination.pageTotal = res.data.data.total;
                    } else {
                        this.loading = false;
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    this.loading = false;
                    console.log(err);
                });
        },
        // 触发搜索按钮
        handleSearch() {
            //this.$set(this.pagination, 'pageIndex', 0);
            this.getData();
        },
        // 分页导航
        pageChange(data) {
            this.pagination = data.backData;
            this.getData();
        },
        //表格组件返回排序对象
        sortChange(data) {
            let column = data.backData;
            //排序
            if (column.order) {
                //倒序
                if (column.order === 'descending') {
                    this.query.sortColumn = column.prop + ' ' + 'desc';
                } else {
                    this.query.sortColumn = column.prop;
                }
            } else {
                //不排序
                this.query.sortColumn = '';
            }
            this.getData();
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.$message.success('删除成功');
                    this.tableData.splice(index, 1);
                })
                .catch(() => {});
        },

        goEdit() {
            if (this.multipleSelection.length < 1) {
                this.$message('请选择一条记录');
                return;
            }
            if (this.multipleSelection.length > 1) {
                this.$message('只能选择一条记录');
                return;
            }
            this.$router.push({
                path: '/addContract',
                query: {
                    id: this.multipleSelection[0].id
                }
            });
        },
        goAdd() {
            this.$router.push({
                path: '/addContract'
            });
        },
        delAllSelection() {
            if (this.multipleSelection.length < 1) {
                this.$message('请选择一条记录');
                return;
            }
            if (this.multipleSelection.length > 1) {
                this.$message('只能选择一条记录');
                return;
            }
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    //请求接口删除数据
                    this.deleteById(this.multipleSelection[0].id);
                })
                .catch(() => {});
        },
        deleteById(id) {
            appMain
                .contractDelById({}, id)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.getData();
                        this.$message.success(res.data.message);
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        // 组件选择完后把数据传过来
        selectFun(data) {
            this.multipleSelection = data.backData;
        },
        //试用到期：
        tryTimeChange(val) {
            console.log('tryTimeChange:', val);
            if (val == null) {
                this.tryTime = [];
                this.query.tryStarTime = '';
                this.query.tryEndTime = '';
                return;
            }
            this.tryTime = val;
            this.query.tryStarTime = val[0];
            this.query.tryEndTime = val[1];
        },
        //合同到期
        overTimeChange(val) {
            console.log('overTimeChange:', val);
            if (val == null) {
                this.overTime = [];
                this.query.overStartTime = '';
                this.query.overEndTime = '';
                return;
            }
            this.overTime = val;
            this.query.overStartTime = val[0];
            this.query.overEndTime = val[1];
        },
        //树组件搜索结果返回
        searchBack(data) {
            console.log(data.backData, 'searchTreeBack');
            this.query.employeeName = data.backData.name;
            this.query.employeeId = data.backData.id;
            this.showSearch = false;
        },
        closeTreeDialog(data) {
            this.showSearch = data.showDialog;
        },
        /**
         * 下拉框数据start
         */
        dictItemForPerfection(item) {
            appMain.dictItem({ typeName: item }).then(res => {
                if (res.data.code === 20000) {
                    if (item == '合同状态') {
                        this.contractStatus = res.data.data;
                    } else if (item == '合同类型') {
                        this.contractType = res.data.data;
                    }
                } else {
                    this.$message.error(res.data.message);
                }
            });
        },
        resetForm(formName) {
            console.log('resetForm');
            this.$refs.tableList.clearSort();
            this.$refs[formName].resetFields();
            this.query.tryEndTime = '';
            this.query.tryStarTime = '';
            this.query.overEndTime = '';
            this.query.overStartTime = '';
            this.query.employeeId = '';
            this.tryTime = [];
            this.overTime = [];
        },
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    this.pagination = { pageIndex: 1, pageSize: 10, pageTotal: 0 };
                    this.getData();
                    //this.resetForm('ruleForm');
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        }
    },

    mounted() {
        var div_height = window.screen.availHeight;
        var height = div_height - 100;
        this.containerHeight = height - 160;
        this.pageTitle = this.$router.currentRoute.meta.title;
        // console.log(this.$router, 'router')
    }
};
</script>

<style scoped>
.container {
    box-sizing: border-box;
}
.handle-box {
    margin-bottom: 20px;
    flex-wrap: wrap;
    font-size: 12px;
}

.handle-select {
    width: 120px;
}

.tree-box {
    border: 1px solid #ddd;
    margin-top: 20px;
    height: calc(100% - 146px);
}
.table-box {
    height: calc(100% - 72px);
}
.el-input {
    max-width: 220px;
}
.el-date-editor {
    width: 100% !important;
}
</style>
